﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLNS_Public;
using QLNS_BLL;

namespace QLNS_GiaoDien
{
    public partial class uc_BacLuong : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_BacLuong()
        {
            InitializeComponent();
        }
        BacLuong_BLL bacluong_bll = new BacLuong_BLL();
        BacLuong_Public bl = new BacLuong_Public();
        private void uc_BacLuong_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = bacluong_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            try
            {
                bl.MaBacLuong = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Bậc Lương"));
                if (XtraMessageBox.Show("Bạn có muốn xóa Mã bậc lương: '" + bl.MaBacLuong + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    bacluong_bll.Delete_BacLuong(bl);
                    uc_BacLuong_Load(sender, e);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể xóa do có bản ghi phụ thuộc","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                bl.HeSo = float.Parse(txt_HeSO.Text);
                bl.LuongCoBan = float.Parse(txt_LuongCB.Text);
                bacluong_bll.Insert_BacLuong(bl,bl);
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                uc_BacLuong_Load(sender,e);
            }
            if (flag == 1)
            {
                bl.MaBacLuong = int.Parse(txt_MaBacLuong.Text);
                bl.HeSo = float.Parse(txt_HeSO.Text);
                bl.LuongCoBan = float.Parse(txt_LuongCB.Text);
                bacluong_bll.Update_BacLuong(bl,bl,bl);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                uc_BacLuong_Load(sender, e);
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txt_MaBacLuong.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Bậc Lương").ToString();
            txt_HeSO.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Hệ Số").ToString();
            txt_LuongCB.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Lương Cơ Bản").ToString();
        }
    }
}
